#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#define FILE_NUM  2

void printFileType(char *path) {
    struct stat stat_buf;
    int ret = stat(path, &stat_buf);
    if (ret < 0) {
        perror("stat error");
        return;
    }

    if (S_ISREG(stat_buf.st_mode)) {
        printf("%s: general file\n", path);
    }
    else if(S_ISDIR(stat_buf.st_mode)) {
        printf("%s: direcotry\n", path);
    }
    else {
        printf("%s: other file type\n", path);
    }
}

int main() {
    char *filepaths[FILE_NUM] = {
        "./file", "./dir"
    };

    int ret = -1;
    for (int i = 0; i < FILE_NUM; ++i){
        if (access(filepaths[i], O_RDONLY) < 0) {
            ret = creat(filepaths[i], 0664);

            if (ret < 0) {
                perror("creat error");
                exit(1);
            }
        }
    }

    for (int i = 0; i < FILE_NUM; ++i) {
        printFileType(filepaths[i]);

    }
    return 0;
}
